// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

// Implements Filter class
public class LogFilter implements Filter  {
   public void  init(FilterConfig config) 
                         throws ServletException{
      // Get init parameter 
      String testParam = config.getInitParameter("test-param"); 

      //Print the init parameter 
      System.out.println("Test Param: " + testParam); 
   }
   public void  doFilter(ServletRequest request, 
		ServletResponse response,
		FilterChain chain) 
		throws java.io.IOException, ServletException {

			// Get the IP address of client machine.   
			String ipAddress = request.getRemoteAddr();
			String text = "IP "+ ipAddress + ", Time " + new Date().toString();
			System.out.println(text);
			PrintStream out = null;
			try {
				out = new PrintStream(new FileOutputStream("filename.txt"));
				out.print(text);
			}
			finally {
				if (out != null) out.close();
			}
			// Log the IP address and current timestamp.


			// Pass request back down the filter chain
				chain.doFilter(request,response);
	}
	public void destroy( ){
		/* Called before the Filter instance is removed 
		from service by the web container*/
	}
}
